#!/usr/bin/env python

import sys
import time
import socket
from kegg import get_server


def main():
    socket.setdefaulttimeout(120)
    server  = get_server()
    for line in sys.stdin:
        pathway = line.strip().split()[0]
        while True:
            try:
                res = server.get_genes_by_pathway(pathway)
                break
            except Exception, e:
                sys.stderr.write('Error occured: %s , wait..\n' % e)
                time.sleep(10)
                server  = get_server()
        for gene in res:
            sys.stdout.write("%s\t%s\n" % (pathway, gene))
        sys.stdout.flush()
        time.sleep(1)


if __name__ == '__main__':
    main()

